<?php
class RegistrationPresenter {
	private $view;
	private $userRepository;
	public function __construct(RegistrationView $view, IUserRepository $userRepository) {
		$this->view = $view;
		$this->userRepository = $userRepository;
	}
	public function register(User $user, $pass, $passConfirm) {
		if ($pass !== $passConfirm) {
			$this->view->registrationError ( "Passwords don't match. Ha-ha-ha." );
			
			return;
		}
		
		if (strlen ( $pass ) < 6) {
			$this->view->registrationError ( "Can't read imbecile? More than 5 characters!" );
			
			return;
		}
		$user->setPassword ( $pass );
		
		if (strlen ( $user->getName () ) === 0) {
			$this->view->registrationError ( "Strange name yo mofo. No way." );
			return;
		}
		
		if (! filter_var ( $user->getEmail (), FILTER_VALIDATE_EMAIL )) {
			$this->view->registrationError ( "It's not email you idiot!!! Calm down just kidding." );
			return;
		}
		
		if ($this->userRepository->addUser ( $user )) {
			Application::getInstance ()->setActiveUser ( $user );
			$this->view->registrationSucceed ();
		} else {
			$this->view->registrationError ( "Try next time and pick different username!!!! See ya~~~))))" );
		}
	}
}

?>